﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ToDoList.Entity.Models;

namespace NewToDoList.logic
{
    public class OperateMatter
    {
        private static todoContext _ctx = new todoContext();

        public bool AddMatter(int listId, string pastTime, string content)
        {
            int matterId = _ctx.Matter.Max(a => a.MatterId) + 1;
            var list = _ctx.DetailedList.SingleOrDefault(a => a.ListId == listId);
            var matter = _ctx.Matter.SingleOrDefault(a => a.MatterId == matterId);
            if (list != null && matter == null)
            {
                list.Matter = new List<Matter>();
                _ctx.Matter.Add(
                     new Matter { MatterId = matterId, CreatTime = DateTime.Now, PastTime = DateTime.Parse(pastTime), Content = content });
                list.Matter.Add(
                    new Matter { MatterId = matterId, CreatTime = DateTime.Now, PastTime = DateTime.Parse(pastTime), Content = content });
                //list.User.Matter.Add(
                //    new Matter { MatterId = matterId, CreatTime = DateTime.Now, PastTime = DateTime.Parse(pastTime), Content = content });
                matter.ListId = list.User.UserId;
                return true;
            }
            else
                return false;

        }



        public bool DeleteMatter(int matterId, int userId)
        {
            var user = _ctx.Users.SingleOrDefault(s => s.UserId == userId);
            var matter = _ctx.Matter.SingleOrDefault(a => a.MatterId == matterId);
            if (user != null && matter != null)
            {
                _ctx.Matter.Remove(matter);
                return true;
            }
            else
                return false;

        }


        public bool AddRemark(int matterId, string remark)
        {
            var matter = _ctx.Matter.SingleOrDefault(a => a.MatterId == matterId);
            if (matter != null)
            {
                matter.Remark += remark;
                return true;
            }
            else
                return false;

        }

        public bool FinishMatter(int matterId, int listId)
        {
            var matter = _ctx.Matter.SingleOrDefault(a => a.MatterId == matterId);
            if (matter != null)
            {
                matter.Status = true;
                _ctx.SaveChanges();
                return true;
            }
            else
                return false;
        }


        public bool CancelFinishMatter(int matterId)
        {
            var matter = _ctx.Matter.SingleOrDefault(a => a.MatterId == matterId);
            if (matter != null)
            {
                matter.Status = false;
                _ctx.Matter.SingleOrDefault(a => a.MatterId == matterId).Status = false;
                return true;
            }
            else
                return false;
        }


        public List<Matter> ViewAllMatter(int listId)
        {
            var list = _ctx.Matter.Where(m => m.ListId == listId);
            if (list != null)
            {
                return list.ToList();
            }
            else
                return null;

        }

        public List<Matter> ViewUnfinishMatter(int listId)
        {
            var list = _ctx.Matter.Where(m => m.ListId == listId);
            if (list != null)
            {
                return list.Where(a => a.Status == false).ToList();
            }
            else
                return null;
        }

        public List<Matter> OverdueMatterChangeStatus()
        {
            var overdueMatter = _ctx.Matter.Where(a => a.PastTime < DateTime.Now);
            foreach (var item in overdueMatter)
                item.IsOverDue = true;
            var unOverdueMatter = _ctx.Matter.Where(a => a.PastTime > DateTime.Now);
            foreach (var item1 in unOverdueMatter)
                item1.IsOverDue = false;
            if (overdueMatter != null)
            {
                _ctx.SaveChanges();
                return overdueMatter.ToList();              
            }
            else
                
                return null;

        }

    }

}
